package com.aryankh.controller;

import com.aryankh.annotation.HasPermission;
import com.aryankh.annotation.HasRole;
import com.aryankh.annotation.Log;
import com.aryankh.annotation.Repeat;
import com.aryankh.entity.KhUser;
import com.aryankh.service.KhUserService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * 用户信息表(KhUser)表控制层
 *
 * @author makejava
 * @since 2022-05-28 07:19:50
 */
@RestController
@RequestMapping("khUser")
public class KhUserController extends BaseController{
    /**
     * 服务对象
     */
    @Resource
    private KhUserService khUserService;

    /**
     * 分页查询
     *
     * @param khUser 筛选条件
     * @return 查询结果
     */
    @GetMapping
    @Log(title="查询用户",bussinessType="用户操作")
    @Repeat(100)
    public ResponseEntity<Page<KhUser>> queryByPage( KhUser khUser) {

        return ResponseEntity.ok(this.khUserService.queryByPage(khUser, PageRequest.of(khUser.getPage(), khUser.getSize())));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    @HasPermission("system:user:query")
    public ResponseEntity<KhUser> queryById(@PathVariable("id") Long id) {

        return ResponseEntity.ok(this.khUserService.queryById(id));
    }


    @GetMapping("getInfo")
    public ResponseEntity<HashMap<String, List<String>>> getInfo(KhUser khUser) {
        return ResponseEntity.ok(this.khUserService.getInfo());
    }

    /**
     * 新增数据
     *
     * @param khUser 实体
     * @return 新增结果
     */
    @PostMapping
    @HasRole({"hr","admin"})
    @Log(title="创建用户",bussinessType="用户操作")
    @Repeat
    public ResponseEntity<KhUser> add(KhUser khUser) {
        return ResponseEntity.ok(this.khUserService.insert(khUser));
    }

    /**
     * 编辑数据
     *
     * @param khUser 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<KhUser> edit(KhUser khUser) {
        return ResponseEntity.ok(this.khUserService.update(khUser));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Long id) {
        return ResponseEntity.ok(this.khUserService.deleteById(id));
    }

}

